package com.lavida.project.system.user.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;
import com.sun.jna.platform.win32.WinUser.MSG;

@Controller
@RequestMapping("system/setting")
public class SettingController {
	@Autowired
	private IUserService userService;
	private String prefix = "system/setting";
	@GetMapping()
	public String setting(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix+"/setting";
	}
	@RequestMapping("/resetPwd")
	@ResponseBody
	public Map<String, String> resetPwd(int userId, String oldPassword, String newPassword, String confirmPassword) {
		//System.out.println(userId+","+oldPassword+","+newPassword+","+confirmPassword);
		User user = userService.selectUser(userId);
		Map<String, String> result = new HashMap<String, String>();
		if(newPassword.isEmpty() || confirmPassword.isEmpty()) {
			result.put("status", "19");
			result.put("msg", "密码不能为空！！");
			return result;
		}else if(!newPassword.equals(confirmPassword)) {
			result.put("status", "20");
			result.put("msg", "两次输入的密码不匹配！！");
			return result;
		}else {
			String passwordMD5 = new Md5Hash(user.getLoginName() + oldPassword.trim() + user.getSalt()).toHex();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+passwordMD5);
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+user.getPassword());
			if(passwordMD5.equals(user.getPassword())) {
				String newpasswordMD5 = new Md5Hash(user.getLoginName() + newPassword.trim() + user.getSalt()).toHex();
				try {
					userService.resetPwd(newpasswordMD5, userId);
					result.put("status", "21");
					result.put("msg", "密码更新成功");
					return result;
				} catch (Exception e) {
					result.put("status", "22");
					result.put("msg", "密码更新失败");
					return result;
				}
			}
		}
		result.put("status", "23");
		result.put("msg", "输入的旧密码错误");
		return result;
	}

	@RequestMapping("/basicinformation")
	@ResponseBody
	public Map<String, Object> updateBasicinformation(User user,HttpServletRequest request) {
		Map<String, Object> result = new HashMap<>();
		int s = userService.updateBasicInform(user);
		String msg = "";
		int code = 0;
		if (s==1) {
			msg = "修改成功";
			code = 1;
		}else {
			msg = "修改失败";
			code = 500;
		}
		User user1 = userService.selectUserById(user.getUserId());
		result.put("code", code);
		result.put("msg", msg);
		result.put("user", user1);
		return result;
	}
}
